1 using UnityEngine;
2 using System.Collections;
3
4 public class cameraMotionScript : MonoBehaviour {
5
6 public Transform car;
7 public float distance = 6.4f;
8 public float height = 1.4f;
9 public float rotationDamping = 3.0f;
10 public float heightDamping = 2.0f;
11 public float zoomRacio = 0.5f;
12 public float DefaultFOV = 60;
13 public int mode = 0;
14
15 private Vector3 rotationVector;
16
17 void LateUpdate () {
18 switch(mode)
19 {
20 case 0:
21 var wantedAngel = rotationVector.y;
22 var wantedHeight = car.position.y + height;
23 var myAngel = transform.eulerAngles.y;
24 var myHeight = transform.position.y;
25 myAngel = Mathf.LerpAngle(myAngel, wantedAngel, rotationDamping * Time.deltaTime);
26 myHeight = Mathf.Lerp(myHeight, wantedHeight, heightDamping * Time.deltaTime);
27 var currentRotation = Quaternion.Euler(0, myAngel, 0);
28 transform.position = new Vector3(car.position.x, myHeight, car.position.z);
29 transform.position -= currentRotation * Vector3.forward * distance;
30 transform.LookAt(car);
31 break;
32 case 1:
33 transform.position = new Vector3(car.position.x, car.position.y+2, car.position.z+1);
34 transform.localRotation = car.localRotation;
35 break;
36 case 2:
37 float extrax = 10;
38 float extray = 0;
39 transform.position = new Vector3(Mathf.Clamp(car.position.x, 125 - extrax, 125 + extrax), 250, Mathf.Clamp(car.position.z, 175 - extray, 175 + extray));
40 transform.localRotation = Quaternion.Euler(90,0,0);
41 break;
42 case 3:
43 break;
44 }
45 }
46 void FixedUpdate (){
47 var carrigidbody = car.GetComponent<Rigidbody>();
48 var localVilocity = car.InverseTransformDirection(carrigidbody.velocity);
49 //if (localVilocity.z<-0.5){
50 //rotationVector.y = car.eulerAngles.y + 180;
51 //}
52 //else {
53 rotationVector.y = car.eulerAngles.y;
54 //}
55 float acc = 0;
56 if (mode == 0)
57 {
58 acc = carrigidbody.velocity.magnitude;
59 }
60 GetComponent<Camera>().fieldOfView = DefaultFOV + acc*zoomRacio;
61 }
62 }